package com.hxl.olympic.fragment;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;

import com.hxl.olympic.R;
import com.hxl.olympic.util.Constant;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.sv_search)
    SearchView svSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    private String type = "";

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public void initialize() {
        switch (type) {
            case Constant.GOODS_TYPE:
                svSearch.setQueryHint("查找闲置");
                break;
            case Constant.USER_TYPE:
                svSearch.setQueryHint("查找用户");
                break;
        }

    }

    @Override
    public void initViews() {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }

    @Override
    public void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_search;
    }


    @Override
    @OnClick({R.id.img_back, R.id.tv_search})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                try {
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            fragmentManager.popBackStack();
                        }
                    }, 500);
                }
                break;
            case R.id.tv_search:
                try {
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (type.equals(Constant.USER_TYPE)) {

                } else {
                    String input = svSearch.getQuery().toString();
                    SelectGoodsFragment selectGoodsFragment = new SelectGoodsFragment();
                    selectGoodsFragment.setInput(input);
                    fragmentManager.popBackStack();
                    fragmentManager.beginTransaction().replace(R.id.layout_main, selectGoodsFragment)
                            .addToBackStack(null)
                            .commit();
                }
                break;
        }
    }
}
